package com.eastcom.groovy.tools.file

import com.eastcom.groovy.Launcher
import com.eastcom.groovy.config.GroovyScript

import org.codehaus.groovy.tools.FileSystemCompiler

/**
 * Created by yuanyue on 2014-08-07.
 */
class GroovyUtil {
    private static List<String> pathList = new ArrayList()
    private static GroovyClassLoader classLoader = new GroovyClassLoader(Launcher.class.getClassLoader())

    public static List<Object> InvokeGroovyShells(String[] pathes, String[] shells, Object[] paramValues) {
        pathes.each {
            if (!pathList.contains(it)) {
                pathList.add(it)
            }
        }
        String[] pathArr = new String[pathList.size()]
        int n = 0
        pathList.each {
            pathArr[n++] = it
        }
        GroovyScriptEngine groovyScriptEngine = new GroovyScriptEngine(pathArr)
        List<Object> resultList = new ArrayList()
        for (int i = 0; i < shells.length; i++) {
            String shell = shells[i]
            Object paramValue = null
            if (paramValues != null && i < paramValues.length) {
                paramValue = paramValues[i]
            }
            Binding binding = new Binding()
            binding.setVariable("args", paramValue)
//            groovyScriptEngine.getConfig().scriptExtensions.add(shell[i])
            Object res = groovyScriptEngine.run(shell, binding)
            resultList.add(res)
        }
        return resultList
    }

    public static List<Object> InvokeGroovyShells(List<GroovyScript> scriptList) {
        List<String> pathList = new ArrayList()
        scriptList.each {
            if (!pathList.contains(it.scriptFileRootPath)) {
                pathList.add(it.scriptFileRootPath)
            }
        }
        String[] pathes = new String[pathList.size()]
        int a = 0;
        pathList.each {
            pathes[a] = it
            a++
        }
        println pathes[0]
        println pathes.length
//        classLoader.loadClass()
        GroovyScriptEngine groovyScriptEngine = new GroovyScriptEngine(pathes)
        List<Object> resultList = new ArrayList()
        for (int i = 0; i < scriptList.size(); i++) {
            GroovyScript shell = scriptList[i]
            Object paramValue = shell.param
            Binding binding = new Binding()
            binding.setVariable("args", paramValue)
//            groovyScriptEngine.getConfig().scriptExtensions.add(shell[i])
            println shell.scriptFileName
            Object res = groovyScriptEngine.run(shell.scriptFileName, binding)
//            groovyScriptEngine.invokeMethod()
            resultList.add(res)
        }
        return resultList
    }

    public static Class loadScriptClass(GroovyScript script) {
        Class<?> groovyClass = null;
        String classPath = script.scriptFileRootPath+"/"+script.scriptFileName
        String classKey = String.valueOf(classPath.hashCode());
        //先从缓存里面去Class文件
        if(GroovyScriptClassCache.newInstance().containsKey(classKey)){
            groovyClass = GroovyScriptClassCache.newInstance().getClassByKey(classKey);
        }else{
            groovyClass = classLoader.parseClass(new File(classPath))
            GroovyScriptClassCache.newInstance().putClass(classKey, groovyClass);
        }
        return groovyClass
    }
}